<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<base href="<%=basePath%>">
    <title>自动生成试卷</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<link rel="stylesheet" type="text/css" href="js/easyui/themes/default/easyui.css">
	<script type="text/javascript" src="../../js/jquery-2.0.0.js"></script>
	<script type="text/javascript" src="js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="js/easyui/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="js/easyui/easyui.dateformat.js"></script>
	<%@include file="../common_resource.jsp" %>
	<style type="text/css">
		.main_body{
			/*position: relative;
            top:190px;*/
			font-size: 14px;
		}
		.table{
			font-size: 14px;
		}
	</style>
	<script type="text/javascript">

        $.getJSON('Kld/selectAllLev/0', function (res) {
            var qxid = $('#questCatagory');

            $.each(res, function (index,item) {
                console.log('%i - %o', index, item);
                qxid.append('<option value="' + item.levKldID +'">' + item.levKldCtgr + '</option>');
            })
        });

        // 加二级知识点下拉列表
        function changeLevKld(ele) {
            var  levKldID= ele.options[ele.selectedIndex].value;
            $.getJSON('Kld/selectAllSecByLevID/0',{levKldID:  levKldID},function (res) {
                var secKldIDFrom = $('#secKldIDFrom');
                secKldIDFrom.empty();
                $.each(res, function (index,item) {
                    console.log('%i - %o', index, item);
                    secKldIDFrom.append('<option value="' + item.secKldID +'">' + item.secKldCtgr + '</option>');
                })
            });
        }


	</script>
	<script>
        // 将选中的知识点添加到提交处
        function addSecTo(secKldIDFrom,secKldIDTo,msg) {

            var secKldIDFrom = $("#" + secKldIDFrom);
            console.log(secKldIDFrom);
            var secKldIDTo = $("#" + secKldIDTo);
            var fromValue = secKldIDFrom.val() == null ? "" : $.trim(secKldIDFrom.val());// 获取select选择的或者text的value
            if (fromValue == ""||fromValue=="请选择..") {
                alert(msg);
                return;
            }
            if(secKldIDTo.find("option[value='"+fromValue+"']")[0]){
                alert("不能重复添加！");
                return;
            }

            var	secKldCtgr = secKldIDFrom.find("option:selected").text();// 获取select选择的text
            var secKldID = secKldIDFrom.find("option:selected").val();// 获取select选择的code
            secKldIDTo.append('<option value="' + secKldID +'">' + secKldCtgr + '</option>');// 为select追加一个option(下拉项)
            secKldIDTo.get(0).selectedIndex = 0;// 设置select索引值为0的项选中

        }
        // 删除select的option
        function delSec(secKldIDTo, msg) {
            var select = $("#" + secKldIDTo);
            var toValue = select.val() == null ? "" : $.trim(select.val());
            if (toValue == "") {
                alert(msg);
            } else {
                select.find("option[value='" + select.val() + "']").remove();// 删除select中选择的option
            }

        }
        function to() {
            location.href="testPaper/selectAllHistoryExamByUserId/${sessionScope.loginUser.sstsUserID}";

        }

	</script>
</head>
<body class="main_body">
<div class="container container-1180" >
	<div class="col" style="margin-top: 10px;">
		<div class="panel panel-info">
			<div class="panel-heading">
				<span ><i class="fa fa-user"></i></span> <label><font style="font-weight: bold;margin-left: 10px;">自动组卷</font></label>
				<label style="float: right;">
					<div class="button_bar">
						<button class="btn btn-info btn-sm" onclick="to()">查看历史测试</button>
					</div>
				</label>
			</div>
			<!-- 主体开始 -->
			<div style="float:left;margin-left: 0px;margin-bottom: 20px">
				<label style="margin-left: 10px"><font color="red"></br>*</font>&nbsp;一级知识点(请单击选择)</label>
				<label id="questCtgrError" class="catagoryMsg"></label>

				<label style="margin-left: 40px"><font color="red"></br>*</font>&nbsp;二级知识点(可多选)</label>
				<label style="margin-left: 48px"><font color="red"></br>*</font>&nbsp;涉及的二级知识点(多选)</label></br>

				<jsp:include page="../question/selectKld.jsp"></jsp:include>
				<br/>
			</div>
                    <br/>
                    <br/>
            <!-- 题型信息div -->
            <div id="qt_div" style="margin-top: 20px">
					<br/>

					<table class="table table-bordered table-hover">
                    <th colspan="8">
						<font color="red">*</font>&nbsp;设置题型信息&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        单选题：<input type="number" name="sQuestionType" id="sQuestion" value="0" placeholder="请输入单选题数量" min="0">
                        判断题：<input type="number" name="dQuestionType" id="dQuestion"  value="0" placeholder="请输入判断题数量" min="0">
                        填空题：<input type="number" name="fQuestionType" id="fQuestion" value="0" placeholder="请输入填空题数量" min="0">
                        简答题：<input type="number" name="shQuestionType" id="shQuestion" value="0" placeholder="请输入简答题数量" min="0">
                        综合题：<input type="number" name="synQuestionType" id="synQuestion" value="0" placeholder="请输入综合题数量" min="0">
                    </th>
                </table>
            </div>
					<!-- 设置试卷信息div -->
					<div id="set_qt_div">
						<table class="table table-bordered table-hover">
							<th colspan="8">
								<font color="red">*</font>&nbsp;设置题目信息&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
								试卷难度：<input type="number" id="paper_difficulty_level" placeholder="试卷难度位于0-1之间" min="0.0" step="0.1" max="1" style="width: 170px;">
								试卷总分：<input type="number" id="total_scores" placeholder="试卷总分(优先题型数量)" min="2" style="width: 170px"><br/>
							</th>

						</table>
						<div class="span12">
							<button class="btn btn-primary" id="generate_paper_button" >生成试卷</button>
						</div>
					</div>
		</div>

	</div>

	</div>
	</div>
</div>
<script>
    $(function(){
        //Set数据结构存放所有题型信息
        qts=new Array();
        //存放所有知识点信息
        kps=new Array();
        //存放传给后台的所有数据
        resultMap={};
        orderFlag=true,scoresFlag=true;

        //设置题型信息
        //点击生成试卷按钮
        $("#generate_paper_button").click(function(){
            generateButtonClick();
        })

    })
    //点击生成试卷按钮
    function generateButtonClick(){

        var paperDiffLev= $("#paper_difficulty_level").val();
        var qtNum,qtScore,qtTotalScore,nunms;
        kps=[];
        
        var selectedvalue = $("#secKldIDTo option:selected").text();
        console.log(selectedvalue)
        $("#secKldIDTo option:selected").each(function(){
            //console.log("sec")
            kps.push($(this).val())
        })

        // 获取题型信息
        var chk_value =[];
        $('input[name="questionType"]:checked').each(function(){
            qts.push($(this).val());
            console.log($(this));
        });

        // 获取题型信息
        var sQuestion = $('#sQuestion').val();
        var dQuestion = $('#dQuestion').val();
        var fQuestion = $('#fQuestion').val();
        var shQuestion = $('#shQuestion').val();
        var synQuestion = $('#synQuestion').val();
        console.log(sQuestion)



        var scores=$("#total_scores").val();
        console.log(scores)
        //先将resultMap清空
        resultMap={};
        resultMap["totalScore"]=scores;
        resultMap["sQuestion"]=sQuestion;
        resultMap["dQuestion"]=dQuestion;
        resultMap["fQuestion"]=fQuestion;
        resultMap["shQuestion"]=shQuestion;
        resultMap["synQuestion"]=synQuestion;

        //第二次改变会保持false
        scoresFlag=true
        if(Number(scores)<=0){
            scoresFlag=false;
        }

        //确认生成试卷--ajax
        //$("#generatePaper").click(function(){
            generatePaper();
        //})
    }

    //生成试卷
    function generatePaper(){
        var paperDiffLev= $("#paper_difficulty_level").val();
        var paperDiffFalg=true;

        if(paperDiffLev==""){
            paperDiffFalg=false;
            alert("请填写试卷难度系数")
        }else{
            paperDiffLev=Number(paperDiffLev);
            //Number.NAN--Not a Number
            if(isNaN(paperDiffLev)){
                alert("试卷难度系数格式错误")
                paperDiffFalg=false;

            }else if(paperDiffLev<0||paperDiffLev>1){
                alert("试卷难度必须位于0-1之间")
                paperDiffFalg=false;
            }

        }

        if(!scoresFlag){
            alert("总分不能为空，请选择试题信息");
        }

        if(paperDiffFalg&&scoresFlag){
            resultMap["paperDiffLev"]=paperDiffLev;

            resultMap["kps"]=kps;

            resultMap["qtSet"]=qts;

            console.log(resultMap)
            var jsonData=JSON.stringify(resultMap);
            //console.log(jsonData)


            $.ajax({
                url:'testPaper/generatePaperAuto',
                method:'post',
                contentType: "application/json; charset=utf-8",
                data:jsonData,
                success:function(data){
                    //alert(data)
                    if(data.rtnCode=="-9999"){
                        alert(data.rtnMessage)
                    }
                    if(data.rtnCode=="0"){
                        if(confirm(data.rtnMessage)){
                            //点击确定按钮
                            location.href="views/exam/test.jsp"
                        }
                    }
                },
                error:function(){
                    alert("error:"+"没有满足此条件的试卷，请重新设置。。。");
                }
            })

        }

    }


</script>

</body>
</html>